package cn.com.nes.common.util;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BimUtils {
	
	private static final String BIM_USERNAME = "15704661884";
	private static final String BIM_PASSWORD = "aptx4869";
	private static String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.WyIwY2MyNjMwZjczYzc0ZGJhOTAyODQ5OWExYjc0ODhkZSIsMTY1Mjg4OTk5MTkwMiwibW9iaWxlIl0.O_tGm1TcxUXNhhB03-enm8Nk8oA5PYj9zBVJvjcxd3Y";
	
	//获取登录状态
	private static void getBimLoginStatus(){
		String url = "https://yun.uzhujia.com/api/accounts/v1/accounts/authenticate";
		String param = "identityId="+BIM_USERNAME+"&identitySource=mobile&password="+BIM_PASSWORD;
		String result = HttpUtils.sendPost(url, param);
		Map parseObject = JSONObject.parseObject(result, Map.class);
		token = (String) parseObject.get("token");
	}
	
	//获取当前Token
	public static String getToken(){
		//判断Token是否过期
		if(!verifytoken()){
			getBimLoginStatus();
		}
		return token;
	}
	
	//验证Token
	@SuppressWarnings("unchecked")
	private static boolean verifytoken(){
		boolean result = false;
		String url = "https://yun.uzhujia.com/api/accounts/v1/accounts/verifytoken";
		String param = "access_token="+token;
		String sendPost = HttpUtils.sendPost(url, param);
		if(!StringUtils.isEmpty(sendPost)){
			Map<String,Object> parse = (Map<String,Object>) JSONObject.parse(sendPost);
			if((Boolean) parse.get("valid")){
				result = true;
			}
		}
		return result;
	}
	
	//获取设计列表
	@SuppressWarnings("unchecked")
	public static List<Map<String,Object>> getDesignList(){
		String token = getToken();
		String url = "https://yun.uzhujia.com/api/erpdata/v1/datasets/zhuxingerp/search?producer=bimdesign&limit=999&skip=0&order=-created";
//		Map<String,Object> map = new HashMap();
//		map.put("meta.valid", 1);
//		map.put("meta.category", "bim-design");
//		map.put("meta.version", 1);
//		map.put("meta.pool", "ab6da264ea664f9c9b2de9fb74a4c59a");
//		map.put("meta.generator", "udesign");
//		map.put("meta.type", "design");
		
		Map<String,String> header = new HashMap<String, String>();
		header.put("x-access-token", token);
		
		byte[] body = "{\"meta.valid\":1,\"meta.category\":\"bim-design\",\"meta.version\":1,\"meta.pool\":\"ab6da264ea664f9c9b2de9fb74a4c59a\",\"meta.generator\":\"udesign\",\"meta.type\":\"design\"}".getBytes();
		String sendPost = HttpUtils.sendPost(url, body,header);
		
		List<Map<String,Object>> parse = (List<Map<String,Object>>) JSONObject.parse(sendPost);
		return parse;
	}
	
	//获取设计ID
	public static String getDesignId(String bimId){
		String token = getToken();
		String url = "https://yun.uzhujia.com/api/erpdata/v1/datasets/zhuxingerp/search?producer=bim&limit=1";
//		Map<String,Object> map = new HashMap();
//		map.put("meta.valid", 1);
//		map.put("meta.category", "bim-design");
//		map.put("meta.version", 1);
//		map.put("meta.pool", "ab6da264ea664f9c9b2de9fb74a4c59a");
//		map.put("meta.generator", "udesign");
//		map.put("meta.type", "design");
		
		Map<String,String> header = new HashMap<String, String>();
		header.put("x-access-token", token);
		
		String str = "{\"meta.valid\":1,\"meta.category\":\"bim-bom\",\"meta.version\":1,\"meta.pool\":\"ab6da264ea664f9c9b2de9fb74a4c59a\",\"meta.designId\":\""+bimId+"\"}";
		byte[] body = str.getBytes();
		String sendPost = HttpUtils.sendPost(url, body,header);
		
		List<Map<String,Object>> parse = (List<Map<String,Object>>) JSONObject.parse(sendPost);
		if(parse.size()>0){
			return String.valueOf(parse.get(0).get("dataId"));
		}else{
			return "";
		}
		
	}
	//获取设计清单
	public static Map<String,Object> getDesignDetail(String designId){
		String token = getToken();
		String url = "https://yun.uzhujia.com/api/erpdata/v1/datasets/zhuxingerp/"+designId;
		Map<String,String> header = new HashMap<String, String>();
		header.put("x-access-token", token);
		
		String param = "producer=bim";
		String sendPost = HttpUtils.sendGet(url, param,header);
		
		Map<String,Object> parse = (Map<String,Object>) JSONObject.parse(sendPost);
		return parse;
	}
	public static Map<String,Object> getDesignQingdan(String bimId){
		String designId = getDesignId(bimId);
		Map<String, Object> designDetail = null;
		if(!StringUtils.isEmpty(designId)){
			designDetail = getDesignDetail(designId);
		}
		return designDetail;
	}
	
	public static void main(String[] args) throws UnsupportedEncodingException {
		List<Map<String, Object>> designList = getDesignList();
		System.out.println(designList);
		
	}
}
